package com.wang.wx.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Slf4j
public class DateFormatExample {

    public static SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    public static LocalDateTime parseLocalDateTime(String dateString){
        if (dateString == null || dateString.isEmpty()) {
            return null;
        }
        Date date = null;
        try {
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        date.setHours(date.getHours() - 8);
        // 将Date对象格式化为目标日期字符串
        String formattedDateString = simpleDateFormat.format(date);
        LocalDateTime parse = LocalDateTime.parse(formattedDateString, dateTimeFormatter);
        return parse;
    }
    
    public static Date parseDate(String dateString){
        if (dateString == null || dateString.isEmpty()) {
            return null;
        }
        // 创建一个SimpleDateFormat对象，指定输入格式
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy/MM/dd");

        // 解析原始的日期字符串
        try {
            return inputFormat.parse(dateString);
        } catch (ParseException e) {
            SimpleDateFormat inputFormatYear = new SimpleDateFormat("yyyy");
            try {
                return inputFormatYear.parse(dateString);
            } catch (ParseException ex) {
                log.error("日期格式不符合", e);
                return null;
            }
        }
    }


    public static void main(String[] args) throws ParseException {
        // 原始日期字符串
//        LocalDateTime localDateTime = parseDate("Mon Apr 06 16:30:30 +0000 2009");
//        System.out.println(localDateTime);
//        System.out.println(parseLocalDateTime(null));
//        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy");
//        System.out.println(inputFormat.parse("1949"));

    }
}
